#ifndef     __MESSAGE_QUEUE_H
#define     __MESSAGE_QUEUE_H

#include "../rtx/rtx_os.h"
#include "../utils/nocopyable.h"

namespace rtos {

template <typename M, uint32_t Capacity>
class MessageQueue_t {

public:
    MessageQueue_t(const char *name = "other") {
        osMessageQueueAttr_t attr;
        attr.name =  name;
        attr.attr_bits = 0;
        attr.cb_mem = m_cb_mem;
        attr.cb_size = osRtxMessageQueueCbSize;
        attr.mq_mem = m_mq_mem;
        attr.mq_size = osRtxMessageQueueMemSize(Capacity, sizeof (M));
        m_id = osMessageQueueNew(Capacity, sizeof (M), &attr);
    }

    inline osStatus_t put(const M& val) {
        return osMessageQueuePut(m_id, &val, 0, 0);
    }

    inline osStatus_t poll(M& val, uint32_t timeout = osWaitForever) {
        return osMessageQueueGet(m_id, &val, NULL, timeout);
    }

private:
    osMessageQueueId_t m_id;
    uint64_t m_cb_mem[ (osRtxMessageQueueCbSize + 7) / 8 ];
    uint64_t m_mq_mem[ (osRtxMessageQueueMemSize(Capacity, sizeof (M)) + 7) / 8 ];
};

}


#endif
